﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using ttlang.node;

//拼字符串模组
namespace ttlang.builder
{
    //为啥是cplus，我们产生的代码风格是以c为基础，cpp为辅助。
    //所以只取一个plus
    public class builder_cs : IBuilder
    {

        public string Name => "C# SourceCode Builder";
        public BuildTarget type => BuildTarget.CSharp;
        //tt 对应到 CSharp 的基础类型


        public Result BuildNode(Node node, out string result)
        {
            if (node is Node_Expr expr)
            {
                StringBuilder sb = new StringBuilder();
                cs.NodeExporter.BuildNode_Expr(sb, expr);
                result = sb.ToString();
                return null;
            }
            else if (node is Node_CodeAtom code)
            {
                StringBuilder sb = new StringBuilder();
                cs.NodeExporter.BuildNode_Code(sb, code, 0);
                result = sb.ToString();
                return null;
            }

            throw new Exception("not support node:" + node.type);
        }

        public Result BuildModule(semantic.Module module, string outputpath, string temppath, ProjectOption.ProjectType outputType, bool autoBuild, Newtonsoft.Json.Linq.JObject buildOption)
        {
            Result result = new Result(outputpath, temppath);
            result.target = BuildTarget.CSharp;
            ExportNameSpace(result, "", module._defNameSpace);
            cs.ProjectExporter.ExportCSProjFile(outputpath, outputType);
            cs.ProjectExporter.ExportCSProjBaseLib(outputpath);
            if (autoBuild)
            {
                cs.AutoBuild.BuildCSProj(result,outputpath);
                if (result.autobuild == null)
                {
                    result.state = BuildState.FAIL;
                    Console.Write("build fail.");
                }
                else
                {
                    result.state = BuildState.SUCC;
                }
            }
            return result;
        }
        public void ExportNameSpace(Result result, string name, semantic.TTNameSpace _namespace)
        {
            var newname = string.IsNullOrEmpty(name) ? _namespace.name : (name + "_" + _namespace.name);

            if ((_namespace.methods != null && _namespace.methods.Count > 0)
                ||
                (_namespace.vars != null && _namespace.vars.Count > 0)
                )
            {
                var membername = newname;
                if (string.IsNullOrEmpty(membername))
                    membername = "__global";
                cs.FileExporter.ExportNameSpaceMember(result, membername, _namespace);
            }
            if (_namespace.namespaces != null)
            {
                foreach (var n in _namespace.namespaces.Values)
                {
                    ExportNameSpace(result, newname, n);
                }
            }
            if (_namespace.types != null)
            {
                foreach (var t in _namespace.types.Values)
                {
                    cs.FileExporter.ExportType(result, newname, t);
                }
            }
        }




    }
}
